﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Transport.Core.Models.ContentManagement;
using Transport.Core.Models.Users;
using Transport.Infrastructure.Mvc;
using Transport.Infrastructure.Services;
using Transport.Models;
using Transport.Services;
using Transport.Services.ContentManagement;
using Transport.Services.Customers;
using Transport.Services.Infrastructure;
using Transport.Services.Users;

namespace Transport.Controllers
{
    public class HomeController : ControllerBase<IUserRepository, User>
    {
        private readonly IMailServerService _mailServerService;
        private readonly IStaticPageRepository _staticPageRepository;
        private readonly INewsItemRepository _newsItemRepository;
        private readonly ICustomerLogoRepository _customerLogoRepository;

        public HomeController(IUnitOfWork unitOfWork, IMailServerService mailServerService,
                              IUserRepository repository, IStaticPageRepository staticPageRepository,
                              INewsItemRepository newsItemRepository, ICustomerLogoRepository customerLogoRepository)
            : base(repository, unitOfWork)
        {
            _mailServerService = mailServerService;
            _staticPageRepository = staticPageRepository;
            _newsItemRepository = newsItemRepository;
            _customerLogoRepository = customerLogoRepository;
        }
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var listNews = _newsItemRepository.GetAll().OrderByDescending(p => p.NewsItemId).Take(8).ToList();
            var cutomerLogos = _customerLogoRepository.GetAll().ToList();
            ViewBag.ListNews = listNews;
            ViewBag.CustomerLogos = cutomerLogos;
            return View();
        }

        public ActionResult PrivacyPolicy()
        {
            return View();
        }

        public ActionResult Contact()
        {
            const int pageType = (int)EnumStaticPages.Contact;
            var model = _staticPageRepository.GetAll().FirstOrDefault(p => p.Type == pageType);
            model.Content = HttpUtility.HtmlDecode(model.Content);
            return View(model);
        }

        public ActionResult About()
        {
            const int pageType = (int)EnumStaticPages.About;
            var model = _staticPageRepository.GetAll().FirstOrDefault(p => p.Type == pageType);
            model.Content = HttpUtility.HtmlDecode(model.Content);
            return View(model);
        }

        [HttpPost]
        public ActionResult HelpAndSupport(HelpAndSupportModel model)
        {
            //Send email
            var emailSupport = ConfigurationManager.AppSettings["EmailSupport"];
            string dataPath = System.Web.HttpContext.Current.Server.MapPath("~/Content/EmailTemplates");
            /*
            //Send Mail To User
            // 0: SupportRef 
            // 1: Url Host
            string mess = "";
            IniFile ini = new IniFile(dataPath + "\\SendMailConfirmSupportUser.ini");
            var title = ini.GetStringValue("Title");
            var subject = ini.GetStringValue("Subject");
            var content = ini.GetStringValue("Content");
            object[] obj = new object[5];
            //obj[0] = entity.HelpSupportId.ToString();
            obj[1] = WebHelpers.GetUrlHost();
            _mailServerService.SendByServer(model.Email, subject, string.Format(content, obj), null, ref mess);

             */

            //Send Mail Supper Admin
            // 0: Title 
            // 1: Date sumitted 
            // 2: Name 
            // 3: Email Address 
            // 4: Message 
            var messAdmin = "";
            var iniAdmin = new IniFile(dataPath + "\\SendMailSupportToSupperAdmin.ini");
            //var titleAdmin = iniAdmin.GetStringValue("Title");
            var subjectAdmin = iniAdmin.GetStringValue("Subject");
            var contentAdmin = iniAdmin.GetStringValue("Content");
            var objAdmin = new object[5];
            objAdmin[0] = model.Subject;
            objAdmin[1] = DateTime.Now.ToString("dd/MM/yyyy");
            objAdmin[2] = model.Name;
            objAdmin[3] = model.Email;
            objAdmin[4] = model.Message;
            _mailServerService.SendByServer(emailSupport, subjectAdmin, string.Format(contentAdmin, objAdmin), null, ref messAdmin);

            TempData["MessageSuccess"] = "The message has been sent.";
            return RedirectToAction("Contact");
        }
    }
}
